// pages/my/serviceOrder/serviceOrder.js
const { mockAPI } = require('../../../utils/mockData');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        statusTabs: [
            { label: '全部', value: '' },
            { label: '待付款', value: '待付款' },
            { label: '待服务', value: '待服务' },
            { label: '已完成', value: '已完成' },
            { label: '已取消', value: '已取消' }
        ],
        currentStatus: '',
        orderList: [],
        pageNum: 1,
        pageSize: 10,
        hasMore: true,
        loading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let status = '';
        if (options.status) {
            status = options.status;
        }
        this.setData({
            currentStatus: status,
            pageNum: 1,
            orderList: [],
            hasMore: true
        });
        this.loadOrders(true);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // 返回后刷新
        this.loadOrders(true);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({ pageNum: 1, orderList: [], hasMore: true });
        this.loadOrders(true);
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (this.data.hasMore && !this.data.loading) {
            this.setData({ pageNum: this.data.pageNum + 1 });
            this.loadOrders();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    onTabChange(e) {
        const status = e.currentTarget.dataset.status;
        this.setData({ currentStatus: status, pageNum: 1, orderList: [], hasMore: true });
        this.loadOrders(true);
    },

    loadOrders(refresh = false) {
        if (this.data.loading || (!this.data.hasMore && !refresh)) return;
        this.setData({ loading: true });
        mockAPI.getOrderList({
            status: this.data.currentStatus,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }).then(res => {
            const list = refresh ? res.data.list : this.data.orderList.concat(res.data.list);
            this.setData({
                orderList: list,
                hasMore: list.length < res.data.total,
                loading: false
            });
        });
    },

    onDetail(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({ url: '/pages/my/orderDetail/orderDetail?id=' + id });
    },

    onCancel(e) {
        const id = e.currentTarget.dataset.id;
        wx.showModal({
            title: '提示',
            content: '确定要取消该订单吗？',
            success: (res) => {
                if (res.confirm) {
                    mockAPI.cancelOrder(id).then(() => {
                        wx.showToast({ title: '已取消', icon: 'success' });
                        this.loadOrders(true);
                    });
                }
            }
        });
    },

    onPay(e) {
        const id = e.currentTarget.dataset.id;
        const order = this.data.orderList.find(o => o.orderId === id);
        
        // 模拟支付
        mockAPI.payOrder({ orderId: id, paymentType: '微信' }).then(() => {
            wx.showToast({ title: '支付成功', icon: 'success' });
            this.loadOrders(true);
            
            // 支付成功后跳转到聊天页面
            setTimeout(() => {
                wx.navigateTo({ 
                    url: `/pages/my/chat/chatDetail?orderId=${order.orderId}&lawyerName=${order.lawyerName}` 
                });
            }, 1500);
        });
    },

    onComplete(e) {
        const id = e.currentTarget.dataset.id;
        wx.showModal({
            title: '确认完成',
            content: '请确认服务已完成',
            success: (res) => {
                if (res.confirm) {
                    mockAPI.completeOrder(id).then(() => {
                        wx.showToast({ title: '已完成', icon: 'success' });
                        this.loadOrders(true);
                    });
                }
            }
        });
    },

    onComment(e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({ url: '/pages/my/orderComment/orderComment?id=' + id });
    }
})